package com.detection.motion.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.detection.motion.bean.Device;
import com.detection.motion.bean.DeviceConfig;
import com.detection.motion.bean.Status;
import com.detection.motion.service.DeviceConfigService;
import com.detection.motion.service.DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/deviceConfig")
public class DeviceConfigController {

    @Autowired
    DeviceConfigService deviceConfigService;

    @Autowired
    DeviceService deviceService;

    @PostMapping("/setOrUpdateDeviceConfig")
    public Status setDeviceConfig(@RequestBody DeviceConfig deviceConfig){
        if (deviceConfig.getDeviceId()==null){
            return new Status("ERROR","设备id不能为空",null);
        }

        if (deviceService.getById(deviceConfig.getDeviceId())==null){
            return new Status("ERROR","设备不存在",null);
        }

        try{
            deviceConfigService.saveOrUpdate(deviceConfig,new UpdateWrapper<DeviceConfig>().eq("device_id",deviceConfig.getDeviceId()));
            return new Status().OK("设备配置成功",deviceConfigService.getOne(new QueryWrapper<DeviceConfig>().eq("device_id",deviceConfig.getDeviceId())));
        }catch (Exception e){
            e.printStackTrace();
            return new Status().ERROR(e);
        }

    }

    @GetMapping("/getDeviceConfigByDeviceId")
    public Status getDeviceConfigByDeviceId(@RequestParam Integer deviceId){
        try{
            DeviceConfig deviceInfo = deviceConfigService.getOne(new QueryWrapper<DeviceConfig>().eq("device_id", deviceId));
            return new Status().OK("设备配置信息查询成功",deviceInfo);
        }catch (Exception e){
            e.printStackTrace();
            return new Status().ERROR(e);
        }
    }

}
